<?php
	
	class Master extends arfaController
	{
		function __construct()
		{
			parent::__construct();
			$this->load->model("master_periode_model");
			$this->load->model("login_model");
		}
		public function grid_periode()
		{
			$data["judul"]			= "Master Periode";
			$data["data_periode"]	= $this->master_periode_model->get();
			$data["content"]		= $this->load->view("v_grid_periode",$data,true);
			$this->load->view("main",$data);
		}
		public function form_periode($id = NULL)
		{
			$this->form_validation->set_rules("tanggal_awal","Tanggal Awal","required");
			$this->form_validation->set_rules("tanggal_akhir","Tanggal Akhir","required");
			$this->form_validation->set_rules("keterangan","Keterangan","required");
			
			if($this->form_validation->run() == FALSE)
			{
				if(!isset($id))
				{
					$data["judul"]			= "Master Periode";
					$data["post_action"]	= "master/form_periode";
					$data["content"]		= $this->load->view("v_form_periode",$data,true);
					$this->load->view("main",$data);
				}
				else
				{
					$data["judul"]			= "Master Periode";
					$data["id"]				= $id;
					$data["data_periode"]	= $this->master_periode_model->get($id);
					$data["post_action"]	= "master/form_periode/".$id;
					$data["content"]		= $this->load->view("v_form_periode",$data,true);
					$this->load->view("main",$data);
				}
			}
			else
			{
				$get_tgl_awal	= $this->input->post("tanggal_awal");
				$get_tgl_akhir	= $this->input->post("tanggal_akhir");
				$get_keterangan	= $this->input->post("keterangan");
				
				$data	= array(
					"TGL_AWAL"		=> date('Y-m-d',strtotime($get_tgl_awal)),
					"TGL_AKHIR"		=> date('Y-m-d',strtotime($get_tgl_akhir)),
					"KETERANGAN"	=> $get_keterangan,
					"STATUS_PERIODE"=> 0
				);
				
				if(!isset($id))
				{
					$this->master_periode_model->save($data);
					echo "
						<script>
							alert('Data berhasil disimpan !');
							document.location = '".site_url('master/grid_periode')."';
						</script>
					";
				}
				else
				{
					$this->master_periode_model->save($data,$id);
					echo "
						<script>
							alert('Data berhasil dirubah !');
							document.location = '".site_url('master/grid_periode')."';
						</script>
					";
				}
			}
		}
		public function form_user()
		{
			$this->form_validation->set_rules("pass_lama","Password Lama","required|callback_cek_pass");
			$this->form_validation->set_rules("pass_baru","Password Baru","required|matches[konf_pass]");
			$this->form_validation->set_rules("konf_pass","Konfirmasi Password Baru","required|matches[pass_baru]");
			
			if($this->form_validation->run() == FALSE)
			{
				$data["judul"]			= "Ganti Password";
				$data["post_action"]	= "master/form_user";
				$data["content"]		= $this->load->view("v_form_user",$data,true);
				$this->load->view("main",$data);
			}
			else
			{
				$get_id_akun	= $this->input->post("id_akun");
				$get_pass_baru	= $this->input->post("pass_baru");
				
				$data = array(
					"PASSWORD" => md5($get_pass_baru)
				);
				
				$this->login_model->save($data,$get_id_akun);
				echo "<script>
					alert('Password Berhasil Dirubah');
					location.reload();
				</script>";
			}
		}
		public function cek_pass($pass_lama)
		{
			$get_id		= $this->session->userdata("id_akun");
			$get_user	= $this->login_model->get($get_id);
			if($get_user["PASSWORD"] == md5($pass_lama)){
				return TRUE;
			} else {
				$this->form_validation->set_message("check_password","Password lama tidak cocok");
				return FALSE;
			}
		}
		
		public function ganti_status($id = NULL)
		{
			$periode	= $this->master_periode_model->get();
			$data = array(
				"STATUS_PERIODE" => 0
			);
			
			foreach($periode as $row)
			{
				$this->master_periode_model->save($data,$row["ID_PERIODE"]);
			}
			$data = array(
				"STATUS_PERIODE" => 1
			);
			
			$this->master_periode_model->save($data,$id);			
			redirect('master/grid_periode');
		}
		
		public function test()
		{
			for($i = 1;$i <= 10;$i++)
			{
				echo "x<br>";
				for($j = 1;$j <= 10;$j++)
				{
					echo "x";
				}
			}
		}
		public function delete_periode($id = FALSE){
			$this->load->model("master_periode_model");
			$this->master_periode_model->delete($id);
			redirect('master/grid_periode');
		}
	}